var viewList = new function() {
    var _this               = this;
    var _listeners          = false;
    var _deleteInProgress   = false;
    
    /**
     * Käynnistää haun.
     */
    this.search = function() {        
        var $search = $("#list-div-search");
        var data    = $("form.form-submit").serializeArray();
                
        // Käynnistä haku
        getSubView($search,"list","table",data);
    }
    
    /**
     * Alustaa näkymän elementit.
     */
    this.initializePage = function() {        
        if (!_listeners) {
            // Käynnistä haku
            $(document).on("click","#list-button-search",function(){
                if (!_deleteInProgress) {
                    _this.search();
                }
            });
            
            // Sivun vaihto
            $(document).on("click",".list-a-pagination",function(){
                if (!_deleteInProgress) {
                    $("#list-input-pos").
                        val($(this).data("pos"));
                    
                    _this.search();
                }
            });
                        
            // Harjoituskategorian muutos
            $(document).on("change","#list-select-exercisecategory",function(){
                var arr = $(this).val().split("|");
                    
                $("#list-select-exerciseid").
                    html(FT.options(ExerciseControl.getCategoryExercises(arr[0],arr[1]))).
                    prepend(
                        $("<option>").
                            attr("value","0").
                            text("- Not selected -")
                    );
            });
            
            // Poista harjoitus
            $(document).on("click",".list-button-delete-workout",function(){
                var _this = this;
                var hashid = $(this).data("hashid");                    
                
                // Vahvistusdialogi
                showConfirm("Delete workout?",function(){
                    if (!_deleteInProgress) {
                        _deleteInProgress = true;
                        
                        // Kutsu rajapintaa
                        apiCall.call(_this,"Workout","delete",{
                            "hashid": hashid
                        });
                    }
                });                                    
            });
            
            // Harjoituksen poisto
            __APICALL_FUNC["Workout/delete"] = function(result) {
                _deleteInProgress = false;
                
                if (result.code == 0) {
                    _this.search();
                } else {
                    setError.call(this,result);
                }
            }
            
            _listeners = true;
        }
        
        $(".list-button").removeAttr("disabled");
    }
}